package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.AppRegistNumStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.AppRegistNumStatsearchRequest;
import com.tuanzi.loan.vo.response.statistics.AppRegistNumStatsearchResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatAppRegister.STAT_APP_REGISTER;

@SuppressWarnings("unchecked")
@Repository
public class AppRegistNumStatRepositoryImpl extends JOOQBaseRepositoryPlus implements AppRegistNumStatRepository {

    @Override
    protected TableImpl getTable() {
        return STAT_APP_REGISTER;
    }

    @Override
    public Page<AppRegistNumStatsearchResponse> list(AppRegistNumStatsearchRequest searchRequest,Pageable pageable) {

        fields(STAT_APP_REGISTER.fields());
        eq(STAT_APP_REGISTER.PARENT_PRODUCT_ID, searchRequest.getParentProductId());
        between(STAT_APP_REGISTER.CREATE_DATE, searchRequest.getStartDate(), searchRequest.getEndDate());
        orderBy(STAT_APP_REGISTER.CREATE_DATE, Sort.Direction.DESC);

        return search(pageable, record -> {
            AppRegistNumStatsearchResponse response = new AppRegistNumStatsearchResponse();
            response.setId(record.get(STAT_APP_REGISTER.ID));
            response.setDate(record.get(STAT_APP_REGISTER.CREATE_DATE));
            response.setParentProductId(record.get(STAT_APP_REGISTER.PARENT_PRODUCT_ID));
            response.setAppRegisterCount(record.get(STAT_APP_REGISTER.APP_REGISTER_COUNT));
            response.setLoginCount(record.get(STAT_APP_REGISTER.APP_LOGIN_COUNT));
            return response;
        });

    }

}
